package com.wilcom.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wilcom.system.common.BaseResponse;
import com.wilcom.system.common.ErrorCode;
import com.wilcom.system.common.PageRequest;
import com.wilcom.system.common.ResultUtils;
import com.wilcom.system.domain.Notification;
import com.wilcom.system.domain.Repairrequest;
import com.wilcom.system.exception.ThrowUtils;
import com.wilcom.system.model.request.notification.CreateNotification;
import com.wilcom.system.model.request.notification.GetUserNotificationsRequest;
import com.wilcom.system.model.request.notification.IsUnreadMessages;
import com.wilcom.system.model.request.notification.MarkNotificationAsRead;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/notification")
@Slf4j
public class NotificationController {
    @Resource
    private com.wilcom.system.service.NotificationService notificationService;
//- getUserNotifications(userId) - 获取用户的所有通知。 
 
 
    //    public BaseResponse<com.baomidou.mybatisplus.extension.plugins.pagination.Page<Repairrequest>> getAllRepairRequests(@RequestBody PageRequest postQueryRequest) { 
//        long current = postQueryRequest.getCurrent(); 
//        long size = postQueryRequest.getPageSize(); 
//        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR); 
//        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Repairrequest> repairrequestPage = repairrequestService.page(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size), 
//                repairrequestService.getQueryWrapper(postQueryRequest)); 
//        return ResultUtils.success(repairrequestPage);
//    }
    @PostMapping("/getUserNotifications")  
    public BaseResponse<Page<Notification>> getUserNotifications(@RequestBody GetUserNotificationsRequest request) {  
        long current = request.getCurrent();  
        long size = request.getPageSize();  
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Notification> notificationPage = notificationService.page(new   com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size),  
                notificationService.getQueryWrapper(request));  
        return ResultUtils.success(notificationPage);  
//        return notificationService.getUserNotifications(userId); 

    } 
//- markNotificationAsRead(notificationId) - 标记通知为已读。
    @PostMapping("/markNotificationAsRead")
    public BaseResponse<Boolean> markNotificationAsRead(@RequestBody MarkNotificationAsRead markNotificationAsRead) {
        Notification notificationAsRead = new Notification();
        notificationAsRead.setIsRead(1);
        notificationAsRead.setNotificationId(markNotificationAsRead.getNotificationId());
        return ResultUtils.success( notificationService.updateById(notificationAsRead));
    }

    @PostMapping("/createNotification")
    public BaseResponse<Boolean> createNotification(@RequestBody CreateNotification createNotification) {
        return ResultUtils.success( notificationService.createNotification(createNotification));
    }
    @PostMapping("/isUnreadMessages")
    public BaseResponse<Boolean> isUnreadMessages(@RequestBody IsUnreadMessages createNotification) {
        return ResultUtils.success( notificationService.isUnreadMessages(createNotification));
    }
}
